﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;

namespace RAP.Framework.Libary.Utils
{
	/// <summary>
	/// JsonRPCProxy工具对象
	/// </summary>
	public static class JsonRPCProxy
	{
        private static int UniqueId = 0;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="serverUri"></param>
		/// <param name="method"></param>
		/// <param name="data"></param>
		/// <returns></returns>
        public static IDictionary JsonServiceProxy ( string serverUri, string method, IDictionary data )
        {
            string dataJson;
            Hashtable ht = new Hashtable ( );
            IDictionary result;

            WebClient client = new WebClient ( );
            client.BaseAddress = serverUri;
            client.Encoding = Encoding.UTF8;
            client.Headers.Add ( "Content-Type", "text/plain; charset=utf-8" );
            client.Headers.Add ( "X-JSON-RPC", method );

            ht[ "id" ] = JsonRPCProxy.UniqueId++;
            ht[ "method" ] = method;
            ht[ "params" ] = data;
            dataJson = ( (object)ht ).ToJSON ( );
            dataJson = client.UploadString ( serverUri, dataJson );
            result = dataJson.ToDictionary( );
            client.Dispose ( );
            return result;
        }
    }
}
